﻿using System;
using System.IO;
using System.Windows.Forms;

namespace task_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private async void btnFetch_Click(object sender, EventArgs e)
        {
            string url = txtUrl.Text.Trim();

            if (string.IsNullOrEmpty(url))
            {
                MessageBox.Show("没输入URL！");
                return;
            }

            try
            {
                var (phones, emails) = await WebScraper.ExtractPhonesAndEmailsAsync(url);

                if (phones.Count == 0 && emails.Count == 0)
                {
                    MessageBox.Show("页面中不存在手机号或邮箱！");
                    return;
                }

                lstPhones.Items.Clear();
                lstEmails.Items.Clear();

                foreach (var phone in phones)
                    lstPhones.Items.Add(phone);

                foreach (var email in emails)
                    lstEmails.Items.Add(email);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发生错误：{ex.Message}");
            }
        }

        private void btnExportPhones_Click(object sender, EventArgs e)
        {
            if (lstPhones.Items.Count == 0)
            {
                MessageBox.Show("没输入URL！");
                return;
            }

            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.Title = "保存手机号";
            saveDialog.Filter = "文本文件 (*.txt)|*.txt";
            saveDialog.FileName = "phones.txt";

            if (saveDialog.ShowDialog() == DialogResult.OK)
            {
                using (StreamWriter writer = new StreamWriter(saveDialog.FileName))
                {
                    foreach (var item in lstPhones.Items)
                    {
                        writer.WriteLine(item.ToString());
                    }
                }
                MessageBox.Show("手机号导出成功！");
            }
        }
        private void btnExportEmails_Click(object sender, EventArgs e)
        {
            if (lstEmails.Items.Count == 0)
            {
                MessageBox.Show("没输入URL！");
                return;
            }

            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.Title = "保存邮箱";
            saveDialog.Filter = "文本文件 (*.txt)|*.txt";
            saveDialog.FileName = "emails.txt";

            if (saveDialog.ShowDialog() == DialogResult.OK)
            {
                using (StreamWriter writer = new StreamWriter(saveDialog.FileName))
                {
                    foreach (var item in lstEmails.Items)
                    {
                        writer.WriteLine(item.ToString());
                    }
                }
                MessageBox.Show("邮箱导出成功！");
            }
        }

        private void lblPhones_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }


}

